package com.foreveross.bsl.common.utils.balance.cxf;

import java.util.List;
import java.util.Random;

/**
 * load balance strategy based on a randomized walk through the
 * static cluster represented by multiple endpoints associated
 * with the same service instance.
 *
 * @author Vector Ho  Date:2014-10-17 23:26
 * @see org.apache.cxf.clustering.RandomStrategy
 */
public class RandomLoadBalanceStrategy extends AbstractLoadBalanceStrategy {
    private Random random;

    /**
     * Constructor.
     */
    public RandomLoadBalanceStrategy() {
        random = new Random();
    }

    /**
     * Get next alternate endpoint.
     * It will not remove the address.
     * 
     * @param alternates non-empty List of alternate endpoints
     * @return next alternate
     */
    protected <T> T getNextAlternate(List<T> alternates) {
        return alternates.get(random.nextInt(alternates.size()));
    }
}
